<template>
<div class="about">
  <h2>x: {{x}}, y: {{y}}</h2>
  <hr>
  <h2 v-if="loading">正在加载中...</h2>
  <h2 v-else-if="errorMsg">{{errorMsg}}</h2>
  <!-- <img v-else :src="result.message" alt=""> -->
  <img v-else-if="result"  :src="result[0].url" alt="">
</div>
</template>

<script lang="ts">
import useClickPosition from '@/hooks/useClickPosition'
import useUrlLoader from '@/hooks/useUrlLoader'

import {watch} from 'vue'

interface IDog {
  message: string
  status: string
}

interface ICat {
  id: string
  url: string
}

export default {

  // mounted () {}
  
  setup() {
    const {x, y} = useClickPosition()
    // const {result, errorMsg, loading} = useUrlLoader<IDog>('https://dog.ceo/api/breeds/image/random')
    const {result, errorMsg, loading} = useUrlLoader<ICat[]>('https://api.thecatapi.com/v1/images/search')

  watch(result, (value) => {
    if (value) {
      console.log(value[0].url)
    }
  })

    return {
      x,
      y,
      result, 
      errorMsg, 
      loading
    }
  }
}
</script>